
const aesjs = require('aes-js');
const {getHeader} = require('./duozhuayu');


var helper = {
	    arrayBufferToBase64 : function (str){
        var binary = '';
        var bytes = new Uint8Array( str );
        var len = bytes.byteLength;
        for (var i = 0; i < len; i++) {
            binary += String.fromCharCode( bytes[ i ] );
        }

        // create a buffer
		var buff = Buffer.from(binary, 'utf-8');

		// encode buffer as Base64
		var base64 = buff.toString('base64');
		return base64;

        // return window.btoa( binary );
    },
    base642ArrayBuffer : function (str){
        // var binary_string = window.atob(str);
	var buff = Buffer.from(str, 'base64');

	// decode buffer as UTF-8
	var binary_string = buff.toString('utf-8');

	    var len = binary_string.length;
	    var bytes = new Uint8Array(len);
    	for (var i = 0; i < len; i++)        {
        	bytes[i] = binary_string.charCodeAt(i);
    	}
    	return bytes;
	}  
}


//key DkOliWvFNR7C4WvR iv: GQWKUE2CVGOOBKXU

// var cc = aesjs.utils.utf8.fromBytes(new Uint8Array([71, 81, 87, 75, 85, 69, 50, 67, 86, 71, 79, 79, 66, 75, 88, 85]));

// console.log(cc);

// var str2 = "4123412341234";
//  var headers = getHeader(str2);


// //加密
var iv = new Uint8Array([71, 81, 87, 75, 85, 69, 50, 67, 86, 71, 79, 79, 66, 75, 88, 85]);
var key = new Uint8Array([68, 107, 79, 108, 105, 87, 118, 70, 78, 82, 55, 67, 52, 87, 118, 82]);

// var origKey = 'QVFJRGFzZGNmYXNkYXNkeA==';
// var key = helper.base642ArrayBuffer(origKey);
// var value = str2;


var value = "1618885206616:4123412341234:48354949";



var valueAsBytes = aesjs.utils.utf8.toBytes(value);
// var paddedData = aesjs.padding.pkcs7.pad(valueAsBytes);
// console.log(paddedData);

var aesEcb = new aesjs.ModeOfOperation.cfb(key,iv);
// console.log("list",paddedData);
var encryptedBytes = aesEcb.encrypt(valueAsBytes);        
 
const token = aesjs.utils.hex.fromBytes(encryptedBytes);
 console.log("aaaaaa encryptedBytes:",token,[key,iv,valueAsBytes]);




// var value = 'wqFXwobCmWTDocOVwploUMOHwpUxw6Y7LQ==';

// var valueAsBytes = helper.base642ArrayBuffer(value);

// console.log('valueAsBytes : ', valueAsBytes);        

// var aesEcb = new aesjs.ModeOfOperation.ecb(key);

// var decryptedBytes = aesEcb.decrypt(valueAsBytes); 

// var decryptedText = aesjs.utils.utf8.fromBytes(decryptedBytes);

// console.log('decrypt result : ',decryptedText);






